<template>
  <div class="backColor">
    <div class="swiper">
      <van-swipe :autoplay="3000" indicator-color="#CAA674">
        <van-swipe-item
          v-for="(item, index) in info.data.listAlbum"
          :key="index"
          @click="seeImage(index)"
        >
          <img :src="item" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="content">
      <div class="title">{{ info.data.name }}</div>
      <div class="number">商品编号：{{ info.data.number }}</div>
      <div class="money">
        <span>售出价格</span>
        <div>
          ￥<span>{{ info.data.price }}</span>
        </div>
      </div>
    </div>
    <div class="details">
      <div class="text">商品详情</div>
      <div class="html" v-html="info.data.detail"></div>
    </div>
    <div class="buttons">
      <div class="left" @click="router.go(-1)">返 回</div>
      <div class="right" @click="submit">
        {{ info.buttonTxt }}
      </div>
    </div>
  </div>
  <van-popup
    v-model:show="show"
    @click-overlay="clickOverlay"
    style="width: 80%; background-color: var(--backColor)"
  >
    <Popup
      :money="info.data.transferPrice"
      :myId="info.data.id"
      :payType="1"
      @onRmove="onRmove"
    />
  </van-popup>
  <van-dialog
    v-model:show="payCertBool"
    title="是否确认收款"
    show-cancel-button
    confirm-button-text="确认收款"
    @confirm="confirmFn"
    @cancel="payCertBool = false"
    class="dialog"
  >
    <img :src="payCertImg" />
  </van-dialog>
  <van-dialog
    v-model:show="complaintBool"
    title="请说明您的问题"
    show-cancel-button
    confirm-button-text="确认提交"
    @confirm="complaintFn"
    @cancel="complaintBool = false"
    class="dialog2"
  >
    <van-field
      class="dialogField"
      v-model="message"
      rows="2"
      autosize
      label=""
      type="textarea"
      maxlength="100"
      placeholder="请输入内容"
      show-word-limit
    />
  </van-dialog>

  <div
    class="complaintWindow"
    @click="complaintBool = true"
    v-if="info.data.productStatus == 4"
  >
    投诉窗口
  </div>
</template>

<script>
import {
  defineComponent,
  onMounted,
  provide,
  reactive,
  ref,
  getCurrentInstance,
} from "vue";
import { useRouter, useRoute } from "vue-router";
import { ImagePreview } from "vant";
import Popup from "@/components/Popup.vue";
import {
  getUserDetail,
  imgUrl,
  getPostStatus,
  getChangeStatus,
  receiptCert,
  getPayCert,
  getPayment,
  saveComplaint,
} from "@/api/http.js";

export default defineComponent({
  setup() {
    const router = useRouter();
    const route = useRoute();
    const { proxy } = getCurrentInstance();
    const info = reactive({
      data: {},
      buttonTxt: "",
    });

    const getInfo = async () => {
      const data = await getUserDetail({ id: route.query.orderId });
      data.data.listAlbum = data.data.listAlbum
        .split(",")
        .map((item) => imgUrl + item);
      info.data = data.data;
      getButtonTet();
      console.log("data---", data);
    };

    const getButtonTet = () => {
      let txt;
      switch (info.data.productStatus) {
        case "0":
          txt = "取消上架";
          break;

        case "1":
          txt = "已售出";
          break;

        case "2":
          txt = "上架商品";
          break;

        case "3":
          txt = "取消审核";
          break;

        case "4":
          txt = "确认收款";
          break;

        default:
          break;
      }
      info.buttonTxt = txt;
    };

    onMounted(() => {
      getInfo();
    });

    const seeImage = (index) => {
      ImagePreview({
        images: info.data.listAlbum,
        startPosition: index,
      });
    };

    const subBool = ref(false);
    const getBool = async () => {
      for (let i = 1; i <= 3; i++) {
        const { data } = await getPayment({ type: i });
        console.log(subBool.value);
        if (data.image != null) break;
        if (i <= 2) {
          subBool.value = true;
        } else {
          console.log("银行卡支付信息---", data, i);
          if (data.card != null) subBool.value = false;
        }
      }
    };

    const show = ref(false);
    const clickBool = ref(false);
    const payCertBool = ref(false);
    const payCertImg = ref("");
    const submit = async () => {
      console.log(subBool.value);
      if (clickBool.value) return;
      if (subBool.value) {
        proxy.$toast.fail("请先上传支付信息, 2秒后自动跳转...");
        setTimeout(() => {
          router.push({
            path: "/collectPayMoney",
          });
        }, 2000);
        return;
      }
      if (info.data.productStatus == "0") {
        proxy.$dialog
          .confirm({
            title: "是否取消上架",
          })
          .then(async () => {
            const data = await getChangeStatus({
              id: route.query.orderId,
              productStatus: "2",
            });
            if (data.code != 200) return proxy.$toast.fail(data.msg);
            getInfo();
            proxy.$toast.success(data.msg);
            console.log("取消上架---", data);
          })
          .catch(() => {
            console.log("取消");
          });
        return;
      }
      if (info.data.productStatus == "2") {
        const data = await getPostStatus({ id: route.query.orderId });
        console.log(data);
        if (data.code != 200) {
          proxy.$toast(data.msg);
          show.value = true;
          overlayBool.value = false;
          return;
        }
        clickBool.value = true;
        const goodsInfo = await getChangeStatus({
          id: route.query.orderId,
          productStatus: "3",
        });
        console.log("二次上架商品---", goodsInfo);
        proxy.$toast.success(goodsInfo.msg);
        setTimeout(async () => {
          await getInfo();
          clickBool.value = false;
        }, 2000);
        return;
      }
      if (info.data.productStatus == "3") {
        proxy.$dialog
          .confirm({
            title: "是否取消审核",
          })
          .then(async () => {
            clickBool.value = true;
            const data = await getChangeStatus({
              id: route.query.orderId,
              productStatus: "2",
            });
            if (data.code != 200) return proxy.$toast.fail(data.msg);
            proxy.$toast.success(data.msg);
            setTimeout(async () => {
              clickBool.value = false;
              await getInfo();
            }, 2000);
            console.log("取消审核---", data);
          })
          .catch(() => {
            // on cancel
          });
      }
      if (info.data.productStatus == "4") {
        const payCert = await getPayCert({ productId: route.query.orderId });
        payCertImg.value = payCert.data;
        payCertBool.value = true;
        console.log(payCertBool.value);
        console.log(payCertImg.value);
        return;
      }
    };

    const confirmFn = async () => {
      const data = await receiptCert({ id: route.query.orderId });
      if (data.code != 200) return proxy.$toast.fail(data.msg);
      proxy.$toast.success(data.msg);

      setTimeout(async () => {
        payCertBool.value = false;
        await getInfo();
        router.push({
          name: "MyGoods",
        });
      }, 2000);
      console.log("确认收货---", data);
    };

    const onRmove = () => {
      console.log("取消");
      show.value = false;
      getInfo();
    };

    const overlayBool = ref(false);
    provide("overlay", overlayBool);
    const clickOverlay = () => {
      overlayBool.value = true;
    };

    const complaintBool = ref(false);
    const message = ref("");
    const complaintFn = async () => {
      const { msg } = await saveComplaint({
        productId: route.query.orderId,
        content: message.value,
      });
      proxy.$toast.success(msg);
    };

    onMounted(() => {
      getBool();
      info.type = route.query.type;
    });

    return {
      seeImage,
      info,
      router,
      submit,
      show,
      onRmove,
      clickOverlay,
      overlayBool,
      payCertBool,
      confirmFn,
      payCertImg,
      complaintBool,
      complaintFn,
      message,
    };
  },
  components: {
    Popup,
  },
});
</script>

<style lang="less" scoped>
.backColor {
  min-height: 100vh;
  background-color: var(--backColor);

  .swiper {
    width: 100%;
    height: 300px;
    overflow: hidden;

    img {
      height: 300px;
    }
  }
  .content {
    width: 100%;
    padding: 20px 4%;
    background-color: var(--backColor);
    color: var(--textColor);

    .title {
      font-size: 1.125rem;
      line-height: 25px;
      letter-spacing: 0.5px;
      font-family: PingFang SC;
      font-weight: bold;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2; /*想省略几行就写几*/
      -webkit-box-orient: vertical;
    }

    .number {
      margin-top: 20px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: var(--textColorTwo);
    }

    .money {
      margin-top: 20px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      color: var(--textColorTwo);
      display: flex;
      justify-content: space-between;
      align-content: flex-end;

      & > span {
        line-height: 35px;
      }

      div {
        color: #dd0000;

        span {
          font-size: 1.56rem;
        }
      }
    }

    .quantity {
      margin-top: 10px;
      font-size: 0.75rem;
      font-family: PingFang SC;
      font-weight: 400;
      padding-bottom: 20px;
      border-bottom: 1px solid var(--textColorTwo);
    }
  }
  .details {
    width: 100%;
    padding: 20px 4% 60px;
    background-color: var(--backColor);
    color: var(--textColor);

    .html {
      width: 100%;
      overflow: hidden;
      margin-top: 20px;
    }
  }
  .buttons {
    width: 100%;
    height: 60px;
    position: fixed;
    left: 0;
    z-index: 99;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    letter-spacing: 2px;

    div {
      width: 100%;
      height: 100%;
      font-size: 1.125rem;
      font-family: PingFang SC;
      font-weight: bold;
      line-height: 60px;
      text-align: center;
    }

    .left {
      background-color: #fff;
      color: #caa674;
    }
    .right {
      background-color: #caa674;
      color: #fff;
    }
  }
}
.dialog {
  text-align: center;
  img {
    width: 60%;
    margin: 20px 20%;
  }
}
.dialog2 {
  .dialogField {
    margin: 20px auto;
    background-color: var(--backColor);
    color: var(--textColor);
    /deep/ .van-field__control {
      background-color: var(--backColor);
      color: var(--textColor);
    }
  }
}
.complaintWindow {
  position: fixed;
  color: var(--textColor);
  text-align: center;
  line-height: 40px;
  top: 50%;
  right: 0;
  width: 80px;
  height: 40px;
  background-color: #caa674;
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
}
</style>
